﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ApplicationModel;

namespace ApplicationModel.Tests
{
    [TestClass]
    public class EntityTests
    {
        [TestMethod]
        public void PropertyChangingTest()
        {
            bool actual = false;
            var entity = new TestEntity();

            entity.PropertyChanging += (s, e) => actual = true;
            entity.PropertyChangingAwareProperty = "test";

            Assert.IsTrue(actual);
        }

        [TestMethod]
        public void PropertyChangedTest()
        {
            bool actual = false;
            var entity = new TestEntity();

            entity.PropertyChanged += (s, e) => actual = true;
            entity.PropertyChangedAwareProperty = "test";

            Assert.IsTrue(actual);
        }
        
        [TestMethod]
        public void HasErrorsTest()
        { 
            var entity = new TestEntity();

            entity.StringLenghtProperty = "too long string";

            bool actual = entity.HasErrors;

            Assert.IsTrue(actual);
        }

        [TestMethod]
        public void ErrorsTest()
        {
            var entity = new TestEntity();

            entity.StringLenghtProperty = "too long string";

            string actual = entity["StringLenghtProperty"];

            Assert.IsTrue(!string.IsNullOrEmpty(actual));
        }

        [TestMethod]
        public void PropertyErrorTest()
        {
            var entity = new TestEntity();

            entity.StringLenghtProperty = "too long string";

            string actual = entity.Error;

            Assert.IsTrue(!string.IsNullOrEmpty(actual));
        }

        [TestMethod]
        public void RejectChangesEditablePropertyTest()
        {
            var entity = new TestEntity();
            
            entity.EditableProperty = "initial";
            entity.AcceptChanges();
            entity.EditableProperty = "changed";
            entity.RejectChanges();

            Assert.AreEqual("initial", entity.EditableProperty);
        }

        [TestMethod]
        public void AcceptChangesEditablePropertyTest()
        {
            var entity = new TestEntity();

            entity.EditableProperty = "initial";
            entity.AcceptChanges();

            Assert.AreEqual("initial", entity.EditableProperty);

            entity.EditableProperty = "changed";
            entity.AcceptChanges();

            Assert.AreEqual("changed", entity.EditableProperty);
        }

        [TestMethod]
        public void AcceptChangesEditablePropertyTest2()
        {
            var entity = new TestEntity();

            entity.EditableProperty2 = null;
            entity.AcceptChanges();

            Assert.AreEqual(null, entity.EditableProperty2);

            entity.EditableProperty2 = 2;
            entity.AcceptChanges();

            Assert.AreEqual(2, entity.EditableProperty2);
        }

        [TestMethod]
        public void AcceptChangesNonEditablePropertyTest()
        {
            var entity = new TestEntity();

            entity.NonEditableProperty = "initial";
            entity.AcceptChanges();

            Assert.IsNull(entity.NonEditableProperty);
        }

        [TestMethod]
        public void AcceptChangesNonEditableWithInitialValuePropertyTest()
        {
            var entity = new TestEntity();

            entity.NonEditableWithInitialValueProperty = "initial";
            entity.AcceptChanges();
            Assert.AreEqual("initial", entity.NonEditableWithInitialValueProperty);

            entity.NonEditableWithInitialValueProperty = "changed";
            entity.AcceptChanges();

            Assert.AreEqual("initial", entity.NonEditableWithInitialValueProperty);
        }

        [TestMethod]
        public void AcceptChangesNonEditableWithInitialValuePropertyTest2()
        {
            var entity = new TestEntity();

            entity.NonEditableWithInitialValueProperty2 = 1;
            entity.AcceptChanges();
            Assert.AreEqual(1, entity.NonEditableWithInitialValueProperty2);

            entity.NonEditableWithInitialValueProperty2 = 2;
            entity.AcceptChanges();

            Assert.AreEqual(1, entity.NonEditableWithInitialValueProperty2);
        }
    }
}
